﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace project_web2.AdminTool
{
    
    public partial class ManagerAdmin : System.Web.UI.Page
    {
        DAL dal = new DAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Login"] == null) {
                Response.Redirect("Login.aspx");
            } else {
                LoadDataForGridview();
            }
            
        }

        private void LoadDataForGridview() {
            grManageradmin.DataSource = loadInfoAdmins();
            grManageradmin.DataBind();
        }

        private DataTable loadInfoAdmins()
        {
            string sql = "select customerid, customername,username, email, [role], active, ban  from Customers where [role] = 1";
            return dal.get_data(sql);
        }

        protected void grManageradmin_RowCommand(object sender, GridViewCommandEventArgs e) {
              int index = Convert.ToInt32(e.CommandArgument);
            if(e.CommandName == "Xoa"){
                int id = Convert.ToInt32(grManageradmin.DataKeys[index].Value.ToString());
                deleteRow(id);
            } else if (e.CommandName == "Sua") {
                GridViewRow vR = grManageradmin.Rows[index];
                lbNote.Text = "Sua OK" + vR.Cells[1].Text; ;
            } else if (e.CommandName == "Them") {
                lbNote.Text = "them OK" + grManageradmin.DataKeys[index].Value.ToString();
            }
            LoadDataForGridview();
        }
        private void deleteRow(int id) {
            string sql = "delete from Customers where customerid = " + id;
            dal.executeQuery(sql);
        }
        protected void grManageradmin_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)) {
                Button bt = (Button)e.Row.Cells[7].Controls[0];
                bt.OnClientClick = "if(!confirm('Bạn thật sự muốn xóa!')) return false;";
            }
        }


    }
}